//
//  UIWindow+Extension.m
//
//
//  Created by teacher on 15/3/5.
//  Copyright (c) 2015年 itheima. All rights reserved.
//

#import "UIWindow+Extension.h"
#import "HMNewfeatureCollectionViewController.h"
#import "AppDelegate.h"
#import "NSString+GetVCFromSB.h"

@implementation UIWindow (Extension)


- (void)chooseRootViewController
{
    // 3.获取软件自身的版本号
    NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
    NSString *currentVersion = infoDict[@"CFBundleShortVersionString"];

    // 4.获取沙盒中存储的软件版本号
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *sandboxVersion = [defaults objectForKey:@"abc"];
    
    UIWindow *window = ((AppDelegate *)[UIApplication sharedApplication].delegate).window;
    // 5.比较两个版本号
    // 6.如果软件自身的大于沙盒的, 保存软件自身的, 显示新特性
    if ([currentVersion compare:sandboxVersion] == NSOrderedAscending) {
        //  自身的大于沙盒  2.2  2.1
        [defaults setObject:currentVersion forKey:@"abc"];
        [defaults synchronize];
        
        // 显示新特性
        // 一定要传递一个layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置cell的尺寸
        layout.itemSize = [UIScreen mainScreen].bounds.size;
        // 设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // 设置间隙
        layout.minimumLineSpacing = 0;
        
        HMNewfeatureCollectionViewController *newfeatureVc = [[HMNewfeatureCollectionViewController alloc] initWithCollectionViewLayout:layout];
//        self.window.rootViewController = newfeatureVc;
        window.rootViewController = newfeatureVc;
    }else
    {
        QLLoginModel * loginModel = [QLLoginModel accountFromSandbox];
        if (loginModel) {
            UIViewController *vc = [NSString instantiateInitialViewControllerWithSBName:@"Main"];
            self.rootViewController = vc;
        }else{
            UIViewController *vc = [NSString instantiateInitialViewControllerWithSBName:@"Login"];
            self.rootViewController = vc;
        }
        
    }
}
+(UIWindow *) getAppDelegateWindow
{
    return ((AppDelegate *)[UIApplication sharedApplication].delegate).window;
}
@end
